package com.phantom.reflection.clazz;

import com.phantom.Car;

/**
 * 演示获取Class对象的六种方式
 */
public class GetClass_ {
    public static void main(String[] args) throws Exception {

        String classAllPath = "com.phantom.Car";
        Class<?> cls = Class.forName(classAllPath); // 主要用于读取配置文件获取
        System.out.println(cls);

        Class cls2 = Car.class; // 场景：用于参数传递
        System.out.println(cls2);

        Car car = new Car();
        Class cls3 = car.getClass(); // 场景：有实例对象
        System.out.println(cls3);

        // 通过类加载器【4种】获取类的Class对象
        ClassLoader classLoader = car.getClass().getClassLoader();
        Class cls4 = classLoader.loadClass(classAllPath);
        System.out.println(cls4);

        // 基本数据类型按如下方式得到Class对象
        Class<Integer> integerClass = int.class;
        Class<Boolean> booleanClass = boolean.class;

        Class<Integer> integerClass1 = Integer.TYPE;
        Class<Character> characterClass = Character.TYPE;

        System.out.println(integerClass.hashCode());
        System.out.println(integerClass1.hashCode()); // 两者hashcode值相同，自动装箱拆箱


    }
}
